<?php
namespace app\controller;

use think\facade\View;

use app\controller\Base;
use app\model\Article;
use app\model\Industry;
use app\model\ProductsWire;
use app\model\ProductsSurface;
use app\model\ProductsWearPlate;
use app\utils\ReUtils;
use app\model\GuestBook;

class Index extends Base
{
    public function index()
    {   
        return View::fetch();
    }

    public function hello()
    {
        return '正在建设中.....';
    }

    public function about()
    {
        return View::fetch();
    }

    public function development()
    {
        return View::fetch();
    }

    public function honor()
    {
        return View::fetch();
    }

    public function factory()
    {
        return View::fetch();
    }

    public function contactus()
    {
        return View::fetch();
    }

    public function search($key = "")
    {
        $key = $this->request->has('key') ? (string)$this->request->param('key') : '';

        $news_list = $product_list = $industry_list = null;

        if ($key != '') {
            $news_list = Article::where('title|keywords', 'LIKE', '%' . $key . '%')->where('status', 1)->field('id,cid,title,titlepic,username,summary,hits,sort,pubtime')->select();

            $product_list_wire       = ProductsWire::where('title|productstype', 'LIKE', '%' . $key . '%')->field('id, productstype, application')->select();
            $product_list_surface    = ProductsSurface::where('productstype', 'LIKE', '%' . $key . '%')->field('id, productstype, application')->select();
            $product_list_wear_plate = ProductsWearPlate::where('products_number', 'LIKE', '%' . $key . '%')->field('id, products_number, instruction')->select();

            $product_list['product_list_wire']       = $product_list_wire;
            $product_list['product_list_surface']    = $product_list_surface;
            $product_list['product_list_wear_plate'] = $product_list_wear_plate;

            $industry_list = Industry::where('title|keywords', 'LIKE', '%' . $key . '%')->field('id,title,titlepic,summary,create_time')->select();
        }

        return View::fetch('', [
            'news_list'     => $news_list,
            'product_list'  => $product_list,
            'industry_list' => $industry_list
        ]);
    }

    
    /**
     * 留言板
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function saveGuestBook()
    {
        if ($this->request->isPost()) {
            $data = [
                'name'    => $this->request->param('name', '', 'strip_tags'),
                'phone'   => $this->request->param('phone', '', 'strip_tags'),
                'content' => $this->request->param('content', '', 'strip_tags'),
                "company" => $this->request->param('company', '', 'strip_tags'),
                "industry"=> $this->request->param('industry', '', 'strip_tags'),
                "city"    => $this->request->param('city', '', 'strip_tags'),
                "email"   => $this->request->param('email', '', 'strip_tags'),
                'time'    => time()
            ];

            $lang = $this->request->has('lang') ? (string) $this->request->param('lang') : 'zh_cn';

            $renInfos = [
                'name'    => [
                    'zh_cn' => "姓名不能为空",
                    'en'    => "Name can't be empty"
                ],
                'content' => [
                    'zh_cn' => "留言内容不能为空",
                    'en'    => "The message content cannot be empty"
                ],
                'phone'   => [
                    'zh_cn' => "手机号格式不正确",
                    'en'    => "The phone number format is incorrect"
                ],
                'check'   => [
                    'zh_cn' => "验证有误",
                    'en'    => "Error in form validation"
                ],
            ];


            if ($data['name'] == '') {
                return ReUtils::error($renInfos['name'][$lang]);
            }

            if ($data['content'] == '') {
                return ReUtils::error($renInfos['content'][$lang]);
            }

            if ($lang == 'zh_cn') {
                if (preg_phone($data['phone']) == false) {
                    return ReUtils::error("手机号格式不正确");
                }
            }

            $check = check_token($this->request->param());

            if (false === $check) {
                return ReUtils::error($renInfos['check'][$lang]);
            }

            try {
                GuestBook::saveGuestBook($data);
            } catch (\Exception $e) {
                $msg = $e->getMessage();

                return ReUtils::error($msg);
            }
            return ReUtils::success();
        }
    }

    public function token()
    {
        $token = $this->request->buildToken('__token__', 'sha1');
        
        return $token;
    }
    
    public function test()
    {
        return "this is test";
    }
}
